package com.chunfeng.lerunapplication.fragement;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * fragment适配器
 *
 * @author by 春风能解释
 * <p>
 * 2023/12/10
 */
public class FragmentAdapt extends FragmentStateAdapter {
    /**
     * 帧布局集合
     */
    private List<Fragment> fragments;

    /**
     * @param fragmentManager fragment管理器
     * @param lifecycle       生命周期
     */
    public FragmentAdapt(@NonNull @NotNull FragmentManager fragmentManager, @NonNull @NotNull Lifecycle lifecycle, List<Fragment> fragments) {
        super(fragmentManager, lifecycle);
        this.fragments = fragments;
    }

    /**
     * 显示指定位置的fragment
     *
     * @param position 索引
     */
    @NonNull
    @NotNull
    @Override
    public Fragment createFragment(int position) {
        return fragments.get(position);
    }

    /**
     * 个数
     *
     * @return 总数
     */
    @Override
    public int getItemCount() {
        return fragments.size();
    }
}
